package com.gdougms.controller;

import com.gdougms.entity.Equipment;
import com.gdougms.entity.HttpMessage;
import com.gdougms.mapper.EquipmentMapper;
import com.gdougms.service.EquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@Api(description = "体育馆管理系统器材管理接口",tags = {"体育馆管理系统器材管理接口"})
@Slf4j
@RestController
public class EquipmentController {
    @Autowired
    EquipmentService equipmentService;

    @Autowired
    ObjectFactory<HttpMessage> httpMessageObjectFactory;
    synchronized

    @ApiOperation(value = "所有器材查询",notes = "查询所有器材信息")
    @GetMapping("/equipList")
    public HttpMessage getEquipList(){
        HttpMessage httpMessage = httpMessageObjectFactory.getObject();
        List<Equipment> equipList = equipmentService.equipList();
        if (equipList != null){
            httpMessage.setResult(true);
            httpMessage.setObject(equipList);
            httpMessage.setCode(200);
            httpMessage.setMessage("获取器材信息成功！");
        }else {
            httpMessage.setResult(false);
            httpMessage.setCode(-1);
            httpMessage.setMessage("系统异常，获取器材信息失败！");
        }
        return httpMessage;
    }

    @ApiOperation(value = "（模糊）查询单个器材信息",notes = "如：查询所有的篮球信息")
    @GetMapping("/getEquip")
    public HttpMessage getEquipByName(@RequestParam String name){
        HttpMessage httpMessage = httpMessageObjectFactory.getObject();
        List<Equipment> equipmentList = equipmentService.getEquipByName(name);
        if (equipmentList != null){
            httpMessage.setResult(true);
            httpMessage.setObject(equipmentList);
            httpMessage.setCode(200);
            httpMessage.setMessage("获取器材信息成功！");
        }else {
            httpMessage.setResult(false);
            httpMessage.setCode(-1);
            httpMessage.setMessage("系统异常，获取器材信息失败！");
        }
        return httpMessage;
    }


    @ApiOperation(value = "增加器材",notes = "参数：器材名name，器材数number，购入（损坏赔偿）价格cost")
    @PostMapping("/addEquip")
    public HttpMessage addEquip(@RequestParam(required = false) String name,
                                @RequestParam(required = false) Integer number,
                                @RequestParam(required = false) BigDecimal cost,
                                @RequestParam(required = false) String remark){
        int result = equipmentService.addEquip(name, number, cost, remark);

        HttpMessage httpMessage = httpMessageObjectFactory.getObject();
        if (result == 0){
            httpMessage.setMessage("系统异常，添加器材失败");
            httpMessage.setCode(-1);
            httpMessage.setResult(false);
        }else {
            httpMessage.setMessage("添加器材成功！");
            httpMessage.setCode(200);
            httpMessage.setResult(true);
        }
        return httpMessage;
    }

    @ApiOperation(value = "删除器材",notes = "根据器材id")
    @GetMapping("/deleteEquip")
    public HttpMessage deleteEquipById(@RequestParam Integer id){

        int result = equipmentService.deleteEquipById(id);

        HttpMessage httpMessage = httpMessageObjectFactory.getObject();
        if (result == 0){
            httpMessage.setMessage("系统异常，删除器材失败");
            httpMessage.setCode(-1);
            httpMessage.setResult(false);
        }else {
            httpMessage.setMessage("删除器材成功！");
            httpMessage.setCode(200);
            httpMessage.setResult(true);
        }
        return httpMessage;
    }

    @ApiOperation(value = "更新器材",notes = "根据器材id修改，ID为必填项")
    @PostMapping("/updateEquip")
    public HttpMessage updateEquip(@RequestParam(required = false) String name,
                                   @RequestParam(required = false) Integer number,
                                   @RequestParam(required = false) BigDecimal cost,
                                   @RequestParam(required = false) String remark,
                                   @RequestParam() Integer id){
        int result = equipmentService.updateEquipById(id, name, number, cost, remark);

        HttpMessage httpMessage = httpMessageObjectFactory.getObject();
        if (result == 0){
            httpMessage.setMessage("系统异常，修改器材信息失败");
            httpMessage.setCode(-1);
            httpMessage.setResult(false);
        }else {
            httpMessage.setMessage("修改器材信息成功！");
            httpMessage.setCode(200);
            httpMessage.setResult(true);
        }
        return httpMessage;
    }

    @ApiOperation(value = "获取所有器材（类型）的名字",notes = "查看有哪些器材")
    @GetMapping("/getAllEquipName")
    public HttpMessage getAllEquipName(){
        HttpMessage  httpMessage = httpMessageObjectFactory.getObject();
        List<String> allName = equipmentService.getAllEquipName();

        if (allName != null){
            httpMessage.setResult(true);
            httpMessage.setMessage("获取器材名列成功！");
            httpMessage.setObject(allName);
            httpMessage.setCode(200);
        }else {
            httpMessage.setResult(false);
            httpMessage.setMessage("获取器材名列失败！");
            httpMessage.setCode(-1);
        }
        return httpMessage;
    }

    @ApiOperation(value = "获取器材的所有价位",notes = "获取表中所有器材的购入价格")
    @GetMapping("/getAllEquipCost")
    public HttpMessage getAllEquipCost(){
        HttpMessage  httpMessage = httpMessageObjectFactory.getObject();
        List<BigDecimal> costs = equipmentService.getAllEquipCost();

        if (costs != null){
            httpMessage.setResult(true);
            httpMessage.setMessage("获取Cost列成功");
            httpMessage.setObject(costs);
            httpMessage.setCode(200);
        }else {
            httpMessage.setResult(false);
            httpMessage.setMessage("获取Cost列失败");
            httpMessage.setCode(-1);
        }
        return httpMessage;
    }
}
